using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_App_Client.MainManagers
{
	#region using
	using DIS_App_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_App_Client.DIS_WS_BusinessLayer;
	#endregion
	public partial class FrmUpdateOrderDetail : Form
	{
		public EventHandler Handler { get; set; }
		private System.Windows.Forms.TextBox txtIDItem;

		public FrmUpdateOrderDetail()
		{
			InitializeComponent();
			InitHiddenFields();
		}

		public void InitHiddenFields()
		{
			txtIDItem = new System.Windows.Forms.TextBox
			{
				Name = "txtIDItem",
				Text = ""
			};
			txtIDItem.Visible = false;
		}

		private void btnSelectItem_Click(object sender, EventArgs e)
		{
            FrmSelectCategory frm = new FrmSelectCategory(true);
            frm.Handler += ShowItemDlg;
            frm.ShowDialog();
		}

        private void ShowItemDlg(object sender, EventArgs e)
        {
            TransferSimpleDataEvent data = (TransferSimpleDataEvent)e;
            FrmManageItem frm = new FrmManageItem(data.Data, true);
            frm.Handler += GetItem;
            frm.ShowDialog();
        }

		public void GetItem(object sender, EventArgs e)
		{
			var eventType = (TransferSimpleDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
            txtIDItem.Text = comp[0];
			txtItem.Text = comp[1];
            var item = BusService.Instance.Item_SelectByID(int.Parse(comp[0]));
            if (item != null)
            {
                txtPrice.Text = item.Price.ToString();
                var localItem = BusService.Instance.LocalPromotion_SelectByIDItemAndIDChemistWareHouse(item.ID, GlobalVariables.g_CurrentUser.IDChemistWareHouse);
                if (localItem != null)
                {
                    var nationalItem = BusService.Instance.NationalPromotion_SelectByIDItem(item.ID);
                    if (nationalItem != null)
                    {
                        if (localItem.Discount <= nationalItem.Discount)
                        {
                            txtDiscount.Text = nationalItem.Discount.ToString();
                        }
                        else
                        {
                            txtDiscount.Text = localItem.Discount.ToString();
                        }
                    }
                    else
                    {
                        txtDiscount.Text = localItem.Discount.ToString();
                    }
                }
                else
                {
                    var nationalItem = BusService.Instance.NationalPromotion_SelectByIDItem(item.ID);
                    if (nationalItem != null)
                    {
                        txtDiscount.Text = nationalItem.Discount.ToString();
                    }
                    else
                    {
                        txtDiscount.Text = string.Empty;
                    }
                }
            }
		}


		private bool ValidateInput(ref string errorText)
		{
			if (txtIDItem.Text == "")
			{
				 errorText = "Please select item";
				return false;
			}
            if (txtQuantity.Text == "")
            {
                errorText = "Please select quantity";
                return false;
            }
            else if(!CommonHelper.IsNumeric(txtQuantity.Text))
            {
                errorText = "Quantity is not numeric";
                return false;
            }
			return true;
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(ref errorText))
			{
				MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			TransferDataInfo(this, new TransferSimpleDataEvent(txtIDItem.Text + "#" + txtQuantity.Text + "#" + txtPrice.Text + "#" + txtDiscount.Text));
		}

		public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
		{
			this.Close();
			this.Handler(this, e);
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}
		private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}
		private void txtDiscount_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}

	}
}